package net.wanglu.www.zzz.rule.frag;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.regex.Pattern;

import net.wanglu.www.zzz.rule.BadEgg;
import edu.stanford.nlp.trees.GrammaticalStructure;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TreeGraphNode;
import edu.stanford.nlp.trees.TypedDependency;

public class AppositiveFragments extends _FragmentsRule {

	Pattern appositive = Pattern.compile("\\bNNS?\\b|\\bNNPS?\\b");
	@Override
	public Object deviseRightSolution(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		sb.clear();
		sb.append("<p>");
		sb.append("修改同位语残缺句有两个方法. 一个方法是因为同位语中含有名词短语, 因此可以很方便地把它当成一个主语, 并添加一个动词作为谓语, 就可以修正这个残缺句了. 例如:");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("误: The unprepared student who was always begging for an extra pencil and a couple sheets of blank paper.");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("正: The unprepared student who was always begging for an extra pencil and a couple sheets of blank paper screamed.");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("另外一个方法是把同位语从句连接到一个主句中. 例如:");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("误: A slacker wasting his afternoon in front of the television.");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("正: A slacker wasting his afternoon in front of the television, Brian opened a bag of potato chips instead of his chemistry textbook.");
		sb.append("</p>");
		return sb.toString();
	}

	@Override
	public Object examine(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		HashMap<?, ?> preprocessData = (HashMap<?, ?>) objects[0];
		String sentence = (String) preprocessData.get("sentence");
		Tree root = (Tree) preprocessData.get("root");
		Collection<?> tdl = (Collection<?>) preprocessData.get("tdl");

		Tree[] rootChildren = root.children();
		Collection<TypedDependency> roots = GrammaticalStructure.getRoots((Collection<TypedDependency>) tdl);
		TreeGraphNode rootDep = roots.iterator().next().dep();
		
		if ( "NP".equals(rootChildren[0].value()) && appositive.matcher(rootDep.label().tag()).find() ) {
			for( Iterator<?> itr = tdl.iterator(); itr.hasNext();) {
				TypedDependency TD = (TypedDependency) itr.next();
				if ( "nsubj".equals(TD.reln().getShortName())) {
					if ( rootDep == TD.gov() || rootDep == TD.dep()) {
						return null;
					}
				}
			}
			BadEgg egg = new BadEgg();
			egg.setViolatedRule(this);
			egg.whereIsWrong = sentence;
			return egg;
		} else {
			return null;
		}
	}

	@Override
	public Object whyItsWrong(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		sb.clear();
		sb.append("<p>");
		sb.append("同位语是一个名词短语或名词性从句, 对另一个名词或代词进行修饰, 限定或说明. 有时一个同位语从句过长, 甚至作者会误认为这是一个完整的句子. 这时, 就会出现一个同位语残缺句. 下面是一些同位语残缺句的例子:");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("The unprepared student who was always begging for an extra pencil and a couple sheets of blank paper.");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("A slacker wasting his afternoon in front of the television.");
		sb.append("</p>");
		return sb.toString();
	}
}
